<?php
 /**
 * 功能描述：	访客统计
 * @datetime	2020-09-23
 * @version   	v1.0
 * @author   	ZHL
 * @copyrigh  	2020 ZHL 版权所有
 */

class C_ClientServe extends Model {

    /**
     * Notes:添加统计信息
     * User: ZhuHaili
     * Date: 2023/12/12
     */
    function addCountInfo($param){

        if(!strtotime($param['date_start'])){
            return array('code'=>'40000','msg'=>'Fail:date_start');
        }
        $param['date_start'] = date('Y-m-d H:i:s', strtotime($param['date_start']));
        if(strtotime($param['date_start']) < strtotime('2022-12-30 00:00:00')){
            return array('code'=>'40001','msg'=>'Fail:date_start');
        }

        usleep(500000); // 延迟0.5秒

        unset($param['id']);
        unset($param['table']);
        $param['date_end'] = date('Y-m-d H:i:s', (strtotime($param['date_start'])+1));
        $param['page_rate'] = intval($param['page_rate']) ? intval($param['page_rate']) : 0;
        $param['from_title'] = urlFromTitle($param['from_url']);

        $model = new C_ClientModel();
        $id = $model->addCountInfo($param);
        if(!$id){
            return array('code'=>'50000','msg'=>'Fail');
        }
        return array(
            'code' => '10000',
            'msg' => 'Success',
            'data' => array(
                'id' => SYS_ENCRYPT($id,'zhlcms'),
                'table' => SYS_ENCRYPT(countTableName(), 'zhlcms')
            )
        );
    }

    /**
     * Notes:修改统计信息
     * User: ZhuHaili
     * Date: 2023/12/12
     */
    public function updateCountInfo($param){
        if(!$param['id']){
            return array('code'=>'40001','msg'=>'Fail');
        }
        if(!$param['table']){
            return array('code'=>'40002','msg'=>'Fail');
        }
        $table = SYS_DECRYPT($param['table'],'zhlcms');
        $where['id'] = SYS_DECRYPT($param['id'], 'zhlcms');
        $data['date_end'] = $param['date_end'];
        $data['page_rate'] = $param['page_rate'];
        $model = new C_ClientModel();
        $res = $model->updateCountInfo($data, $where, $table);
        if(!$res){
            return array('code'=>'50000','msg'=>'Fail');
        }
        return array('code' => '10000', 'msg' => 'Success');
    }

    /**
     * Notes:获取时间段统计
     * User: ZhuHaili
     * Date: 2023/12/14
     * @param $dateStart
     * @param $dateEnd
     * @return array
     */
    public function getDayStat($dateStart, $dateEnd){
        $result = array('code' => '40000', 'msg' => 'Fail', 'data' => array('pvs' => 0, 'uvs' => 0, 'ips' => 0));
        if(!strtotime($dateStart)){
            return $result;
        }
        if(!strtotime($dateEnd)){
            return $result;
        }
        $dateStart = date('Y-m-d 00:00:00', strtotime($dateStart));
        $dateEnd = date('Y-m-d 23:59:59', strtotime($dateEnd));
        $model = new C_ClientModel();
        $res = $model->getDayStat($dateStart, $dateEnd);
        if(!$res){
            $result['code'] = '50000';
            return $result;
        }
        return array(
            'code' => '10000',
            'msg' => 'Success',
            'data' => array(
                'pvs' => ($res['pvs'] ? $res['pvs'] : 0),
                'uvs' => ($res['uvs'] ? $res['uvs'] : 0),
                'ips' => ($res['ips'] ? $res['ips'] : 0)
            )
        );
    }

    /**
     * Notes:获取最近几日走势图
     * User: ZhuHaili
     * Date: 2023/12/14
     * @param $dateStart
     * @param $dateEnd
     * @return array
     */
    public function getRecentTrend($dateStart, $dateEnd){
        $result = array();
        if(!strtotime($dateStart)){
            return $result;
        }
        if(!strtotime($dateEnd)){
            return $result;
        }
        $dateStart = date('Y-m-d 00:00:00', strtotime($dateStart));
        $dateEnd = date('Y-m-d 23:59:59', strtotime($dateEnd));
        $model = new C_ClientModel();
        $res = $model->getRecentTrend($dateStart, $dateEnd);
        if($res){
            foreach($res as $v){
                $result[$v['date']] = $v;
            }
            ksort($result);
        }

        $dataCount = count($result);
        if($dataCount < 7){
            $dataFirstDate = key($result);
            $dataFirstDate = $dataFirstDate ? $dataFirstDate : date('Y-m-d');
            for($i = 1; $i <= (7-$dataCount); $i++){
                $date = date('Y-m-d', strtotime($dataFirstDate . " -$i day"));
                $result[$date] = array('pvs'=>0, 'uvs'=>0, 'ips'=>0);
            }
        }
        ksort($result);
        return $result;
    }

    /**
     * Notes:获取最近30天应用比例
     * User: ZhuHaili
     * Date: 2023/12/14
     */
    public function getAppRatio($dateStart, $dateEnd){
        $result = array();
        if(!strtotime($dateStart)){
            return $result;
        }
        if(!strtotime($dateEnd)){
            return $result;
        }
        $dateStart = date('Y-m-d 00:00:00', strtotime($dateStart));
        $dateEnd = date('Y-m-d 23:59:59', strtotime($dateEnd));
        $model = new C_ClientModel();
        $res = $model->getAppRatio($dateStart, $dateEnd);
        if(!$res){
            return $result;
        }
        return $res;
    }

    /**
     * Notes:今日访客详情
     * User: ZhuHaili
     * Date: 2023/12/14
     */
    public function getDayCountInfos($dateStart, $dateEnd, $countFlag){
        $result = array('code' => '40000', 'msg' => 'Fail');
        if(!strtotime($dateStart)){
            return $result;
        }
        if(!strtotime($dateEnd)){
            $result['code'] = '40002';
            return $result;
        }
        $dateStart = date('Y-m-d 00:00:00', strtotime($dateStart));
        $dateEnd = date('Y-m-d 23:59:59', strtotime($dateEnd));
        $model = new C_ClientModel();
        $res = $model->getDayCountInfos($dateStart, $dateEnd);
        if(!$res){
            $result['code'] = '50000';
            return $result;
        }
        $data['pvs'] = 0;
        $data['uvs'] = 0;
        $data['ips'] = 0;
        if($countFlag == 1){
            $countInfo = $this->getDayStat($dateStart, $dateEnd);
            $data = isset($countInfo['data']) ? $countInfo['data'] : array();
        }
        foreach($res as $k=>$v){
            $res[$k][''] = '';
        }
        $data['list'] = $res;
        $data['sumno'] = count($res);
        $result['code'] = '10000';
        $result['msg'] = 'Success';
        $result['data'] = $data;
        return $result;
    }

    /**
     * Notes:修改访客地区
     * User: ZhuHaili
     * Date: 2023/2/21
     */
    public function updateArea($id, $area){
        $sql['table'] = countTableName();
        $sql['data']['district'] = $area;
        $sql['where']['id'] = $id;
        $r = $this->conn('mysql')->update($sql);
        if($r){
            return array('code'=>'10000','msg'=>'success');
        }
        return array('code'=>'40000','msg'=>'fail');
    }

    /**
     * Notes:根据IP获取页面访问排行榜
     * User: ZhuHaili
     * Date: 2023/12/14
     */
    function getPageIpsCounts($dateStart, $dateEnd, $limit = 0){
        $result = array('code' => '40000', 'msg' => 'Fail');
        if(!strtotime($dateStart)){
            return $result;
        }
        if(!strtotime($dateEnd)){
            $result['code'] = '40002';
            return $result;
        }
        $dateStart = date('Y-m-d 00:00:00', strtotime($dateStart));
        $dateEnd = date('Y-m-d 23:59:59', strtotime($dateEnd));
        $model = new C_ClientModel();
        $list = $model->getPageIpsCounts($dateStart, $dateEnd, $limit);
        $result['data']['list'] = $list;
        $result['data']['sumno'] = count($list);
        $result['code'] = '10000';
        $result['msg'] = '获取成功';
        return $result;
    }

    /**
     * Notes:页面访问排行榜详情
     * User: ZhuHaili
     * Date: 2023/12/14
     */
    function getPageIpsCountInfo($dateStart, $dateEnd, $pageUrl){
        $result = array('code' => '40000', 'msg' => 'Fail');
        if(!strtotime($dateStart)){
            return $result;
        }
        if(!strtotime($dateEnd)){
            $result['code'] = '40002';
            return $result;
        }
        $dateStart = date('Y-m-d 00:00:00', strtotime($dateStart));
        $dateEnd = date('Y-m-d 23:59:59', strtotime($dateEnd));
        $model = new C_ClientModel();
        $list = $model->getPageIpsCountInfo($dateStart, $dateEnd, $pageUrl);
        $result['data'] = $list;
        $result['code'] = '10000';
        $result['msg'] = '获取成功';
        return $result;
    }
}